<?php
// -----------------------------------------------------
// Created by : php-task
// -----------------------------------------------------
// User: tf <1096392101@qq.com>
// -----------------------------------------------------
declare (strict_types=1);

namespace Gao\Task;



use Gao\Task\ipc\Queue;
use Gao\Task\ipc\SharedMemory;

class Progress
{

    //创建子进程
    public function createProgress($callback,...$params)
    {
        $pid = pcntl_fork();
        if ( $pid == -1) {
            // 创建失败
            exit("fork progress error!\n");
        } else if ($pid == 0) {
            // 子进程执行程序
            $pid = posix_getpid(); //获取当前进程的pid;
            $title = "子进程id => $pid";
            cli_set_process_title($title);//为当前进程取一个响亮的名字
            $callback(...$params);
            exit("({$pid})child progress end!\n");
        }else{
            // 父进程执行程序
            return $pid;
        }

    }

    use \Gao\Task\ipc\Pipe;
    public function testPipe()
    {
        for ($i = 0;$i < 3;$i++)
        {
            $pid = pcntl_fork();
            if ( $pid == -1) {
                // 创建失败
                exit("fork progress error!\n");
            } else if ($pid == 0) {
                // 子进程执行程序
                $pid = posix_getpid(); //获取当前进程的pid;
                $title = "子进程id => $pid";
                cli_set_process_title($title);//为当前进程取一个响亮的名字
                echo $title;
                $this->open_write();
                $this->write('id'.$pid);
                echo PHP_EOL;
                exit("({$pid})child progress end!\n");
            }else{
                // 父进程执行程序
                //return $pid;
            }
            $this->open_read();
            echo $this->read();
            sleep(3);
        }
    }

    use SharedMemory;
    public function testMemory()
    {
        $this->memory_init();
        for ($i = 0;$i < 3;$i++)
        {
            $pid = pcntl_fork();
            if ( $pid == -1) {
                // 创建失败
                exit("fork progress error!\n");
            } else if ($pid == 0) {
                // 子进程执行程序
                $pid = posix_getpid(); //获取当前进程的pid;
                $title = "子进程id => $pid";
                cli_set_process_title($title);//为当前进程取一个响亮的名字
                echo $title;
                $this->key = $pid;
                $this->setValue('id=>'.$pid);
                echo PHP_EOL;
                echo $this->getValue();
                exit("({$pid})child progress end!\n");
            }else{
                // 父进程执行程序
                //return $pid;
            }
            sleep(2);
        }
    }

    use Queue;
    public function testQueue()
    {

        for ($i = 0;$i < 3;$i++)
        {
            $pid = pcntl_fork();
            if ( $pid == -1) {
                // 创建失败
                exit("fork progress error!\n");
            } else if ($pid == 0) {
                // 子进程执行程序
                $pid = posix_getpid(); //获取当前进程的pid;
                $title = "子进程id => $pid";
                cli_set_process_title($title);//为当前进程取一个响亮的名字
                $queueId = rand(10000,94502);
                $queue = $this->queue_init($queueId);
                $this->queue_send($title);
                /*$msg = $this->queue_receive();
                echo $msg;*/
                exit("【{$queueId}】---({$pid})child progress end!\n");
            }else{
                // 父进程执行程序
                //return $pid;
            }
            sleep(2);
        }
    }


}